#pragma config FOSC=XT//set fosc
//#pragma config XINST = OFF
#pragma config CANMX = PORTE  //RE4-> CANRX  RE5->CANTX
#include <pic18f66k80.h>
#include "delay.h"
char C_A_H,C_A_L;
char C_F;
int t;
void can_set();
void C_D();
void C_D_1();
void can_set()
{
    TRISEbits.TRISE4=1;//??????CANRX??TRIS??1.
    TRISEbits.TRISE5=0;//???RX?TX CAN?LAT?TRIS?
    CANCON=0X80;//????  ???
    while((CANSTAT&0X80)==0)//CANSTAT??????  ?????
    {
    ;
    }//????????
    BRGCON1=0X01;//BRP=1;  BRP BRGCON1<5:0>->(0~63)
                //Tq=2*(BRP+1)/Fosc
    BRGCON2=0X90;//
    BRGCON3=0X42;// 
    //??????????????????
    TXB0CON=0X03;//TXB0??????????
    
    TXB0SIDH=C_A_H;//?????
    TXB0SIDL=C_A_L&0XE0;//?????
    
    TXB0DLC=0X08;//????
    
    TXB0D0=0X00;
    TXB0D1=0X11;
    TXB0D2=0X22;
    TXB0D3=0X33;
    TXB0D4=0X44;
    TXB0D5=0X55;
    TXB0D6=0X66;
    TXB0D7=0X77;
    
    /*???????*/
    
    RXB0SIDL=C_A_L&0XE0;
    RXB0SIDH=C_A_H;
    
    RXB0CON=0X20;
    RXB0DLC=0X08;
    
    RXB0D0=0X11;
    RXB0D1=0X12;
    RXB0D2=0X13;
    RXB0D3=0X14;
    RXB0D4=0X15;
    RXB0D5=0X16;
    RXB0D6=0X17;
    RXB0D7=0X18;
    
    /*???????*/
    RXF0SIDH=C_A_H;//?????
    RXF0SIDL=C_A_L;
    RXM0SIDH=0XFF;//?????
    RXM0SIDL=0XE0;
    CANCON=0X00;
    //CANCON=0X40;//???????
//    while((CANSTAT&0X40)!=0)
//    {
//    ;
//    }//??????
    while((CANSTAT&0XE0)!=0)
    {
        ;
    }
    PIR5=0X00;
    PIE5=PIE5|0X01;
    IPR5=IPR5|0X01;

}
void C_D()
{
    C_F=0;
    TXB0CON=TXB0CON&0XF7;//????
    TXB0D0=RXB0D0+1;
    TXB0D1=RXB0D1+1;
    TXB0D2=RXB0D2+1;
    TXB0D3=RXB0D3+1;
    TXB0D4=RXB0D4+1;
    TXB0D5=RXB0D5+1;
    TXB0D6=RXB0D6+1;
    TXB0D7=RXB0D7+1;

}
void main()
{
    TRISAbits.TRISA1=0;
    LATAbits.LA1=0;
    PIE1=0;
    PIE2=0;
    PIE3=0;
    PIE4=0;
    PIE5=0;
    C_A_H=0X33;
    C_A_L=0XE0;
    can_set();
    IPEN=1;
    INTCON=0XC0;
    
    while(1)
    {   
       
//        if(C_F==1)C_D();
        if(t>25530)
        {
        C_D_1();
        TXB0CON=TXB0CON|0x08;
        t=0;        
        
        }
         t++;
    
    }




}
void C_D_1()
{
    C_F=0;
    TXB0CON=TXB0CON&0XF7;//????
    TXB0SIDH=C_A_H;//?????
    TXB0SIDL=C_A_L&0XE0;//?????
    TXB0DLC=0X08;//????
    TXB0D0=0x01;
    TXB0D1=0x02;
    TXB0D2=0x03;
    TXB0D3=0x04;
    TXB0D4=0x05;
    TXB0D5=0x06;
    TXB0D6=0x07;
    TXB0D7=0x08;

}
void interrupt HI_ISR()
{
    if(RXB0IF==1)
    {
        C_F=1;
        RXB0IF=0;
        RXB0CONbits.RXB0FUL=0;
    }
}
